/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app.persistence;

import android.content.Context;
import android.content.SharedPreferences;
import at.socialconference.app.connection.ConnectionHandler;

public class Settings {
	
	public static final String PREFS_NAME = "scprops";
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	private static final String SERVERURL = "serverurl";
	
	public static String getUsername(Context cxt){
		SharedPreferences settings =
			cxt.getSharedPreferences(PREFS_NAME, 0);
		return settings.getString(USERNAME, "");
	}
	public static String getPassword(Context cxt){
		SharedPreferences settings =
			cxt.getSharedPreferences(PREFS_NAME, 0);
		return settings.getString(PASSWORD, "");
	}
	public static boolean checkLogin(Context cxt){
		SharedPreferences settings =
			cxt.getSharedPreferences(PREFS_NAME, 0);
		
		boolean ok = settings.contains(PASSWORD) && settings.contains(USERNAME);
		if(ok){
			ConnectionHandler.connect(getUsername(cxt),getPassword(cxt));
		}
		return ok;
	}
	public static void setUsernameAndPassword(Context cxt,String user,String pwd){
		SharedPreferences settings = cxt.getSharedPreferences(PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString(USERNAME, user);
			editor.putString(PASSWORD, pwd);
			editor.commit();
	}
	public static void setServerurl(Context cxt,
			String serverurl) {
		SharedPreferences settings = cxt.getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(SERVERURL, serverurl);
		editor.commit();
		
	}
	public static String getServerURL(Context cxt) {
		SharedPreferences settings =
			cxt.getSharedPreferences(PREFS_NAME, 0);
		return settings.getString(SERVERURL, "192.168.0.105:1981");
	}

}
